 <?php
/*
 *
 * @package MoltX
 * @author $Author$
 * @version $Id$
 * @link $URL$
 * @copyright Copyright (c) 2005-2010, Matthew Oltendorf
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 */

/*
 * @ignore
 */
if ( ! defined ( 'blink' ) )
{
	// Access denied.
	header ( $_SERVER [ 'SERVER_PROTOCOL' ] . ' 403 Forbidden' );
	echo '403 - Forbidden';

	exit;
}

/*
 * Local role class
 * @package Blink
 *
 * Singleton
 */
final class blink_role
{
	/*
	 * Constructor function
	 *
	 * This is private to enforce singleton mode.
	 *
	 * @return null
	 */
	private function __construct ( )
	{
		return null;
	}

	/*
	 * Initiate function
	 *
	 * Start the role.
	 *
	 * @return null
	 */
	public static function initiate ( )
	{
		// Development only. @todo: Remove for production.
		blink_console :: log ( 'role spawned' );

		// Get json input.
		$room = blink :: input ( );

		$link = array ( );
		$sock = array ( );

		$total = count ( $room );

		// Load our storage module.
		include blink_system . '/storage/' . blink :: $storage . '.php';

		// @todo: Check if the room socket(s) exists.
		// Do the room socket(s) exist?
		for ( $files = preg_grep ( '/^[+\\/\\=a-z0-9]+\\.room\\.sock$/', scandir ( blink_system . '/socket/' ) ), $i = 0; $i < $total; ++$i )
		{
			if ( in_array ( $room [ $i ] [ 'channel' ], $files ) )
			{
				$sock [ ] = $files [ $i ];
			}
		}

		$total -= count ( $sock );
		$room = array_diff ( $room, $sock ); // @todo: Use array_splice in a loop as this wont actually work.
		$link = array_merge ( $link, $sock ); // @todo: Use a loop to merge the arrays into one format as the formats will not match.

		// Do we have a cache?
		if ( $total && blink :: $cache )
		{
			include blink_system . '/cache/' . blink :: $cache . '.php';

			// @todo: Get room server(s) from cache.
		}

		// Did we get all the room server(s) from the cache?
		if ( $total )
		{
			// Is everything on this server, or is everything spread around?
			$role = blink :: $handler === null ? 'handler' : 'cache';

			$handler = blink :: connect ( $role );

			// Development only. @todo: Remove for production.
			blink_console :: log ( 'connected to handler' );


			if ( ! $handler )
			{
				// Error.
				throw new Exception ( 'COULD NOT CONNECT TO ' . strtoupper ( $role ) . ' ROLE' );
			}

			// Send request for available room servers.
			$handler -> send ( array (  ) );

			// Development only. @todo: Remove for production.
			blink_console :: log ( 'sent data to handler' );
			
			// Get room server(s) from the handler.
			$response = $handler -> get ( );

			$total -= count ( $response );
			$room = array_diff ( $room, $response ); // @todo: Use array_splice in a loop as this wont actually work.
			$link = array_merge ( $link, $response ); // @todo: Use a loop to merge the arrays into one format as the formats will not match.

			// Development only. @todo: Remove for production.
			blink_console :: log ( 'got data from handler' );

			// Close the connection.
			$handler -> close ( );
		}

		// Connect to the room server(s).

		// Development only. @todo: Remove for production.
		blink_console :: log ( 'role closed' );

		echo '[]';

		return null;
	}
}
